<?php
class SessionMember extends ActiveRecord{

	public function initialize(){
		$this->source = "qo_sessions";
		$this->belongs_to("member");
		$this->belongs_to("group");
	}

	public function contructor($strSessionId, $intMemberId, $intGroupId){
		$this->session_id = $strSessionId;
		$this->qo_members_id = $intMemberId;
		$this->qo_groups_id = $intGroupId;
		$this->date = date("Y-m-d H:i:s");
		$this->ip = $_SERVER['REMOTE_ADDR'];
	}

	/**
	 * getId() Retorna el id de la sesion.
	 *
	 * @access private
	 * @return {string}
	 */
	private function getId(){
		if(isset($_COOKIE['sessionId'])){
			return $_COOKIE['sessionId'];
		}

		if(isset($_GET['sessionId'])){
			if(!isset($_COOKIE['sessionId'])){
				setCookie('sessionId', $_GET['sessionId'], 0, '/');
			}
			return $_GET['sessionId'];
		}

		return null;
	} // end get_id()

	/**
	 * getSessionId() Valida si la sesion es correcta;  si lo es, Retorna el objeto sesionMember.
	 *
	 * @access public
	 * @return {SessionMember}
	 */
	public function getSessionId(){
		$strSessionId = addslashes($this->getId());

		//Validar sessionId no sea nulo
		if($strSessionId == null)
			return null;

		$objSessionMember = $this->find("session_id = '$strSessionId'");

		//Validar que haya iniciado sesion
		if(count($objSessionMember) < 1 || $objSessionMember == null){
			return null;
		}

		return $objSessionMember;
	}
}
?>